package com.stu.manager.personnel.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.manager.config.Result;
import com.stu.manager.course.entity.SysCourse;
import com.stu.manager.course.mapper.SysCourseMapper;
import com.stu.manager.handler.LoginHandler;
import com.stu.manager.organization.entity.SysDepartment;
import com.stu.manager.organization.entity.dto.AddDepartmentDto;
import com.stu.manager.organization.entity.dto.DepartmentDto;
import com.stu.manager.personnel.entity.SysTeacher;
import com.stu.manager.personnel.entity.dto.TeacherDto;
import com.stu.manager.personnel.service.SysTeacherServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-02-14
 */
@RestController
@RequestMapping("/api/teacher")
@CrossOrigin
public class SysTeacherController {

    @Autowired
    private SysTeacherServiceImpl teacherService;
    @Autowired
    private LoginHandler loginHandler;
    @Resource
    private SysCourseMapper courseMapper;
    @PostMapping("pageTeacher/{current}/{limit}")
    @ApiOperation("模糊查询加分页展示教师")
    public Result pageTeacher(@PathVariable Long current,
                                 @PathVariable Long limit,
                                 @RequestBody(required = false) TeacherDto teacherDto,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Page<SysTeacher> pageTeacher = new Page<>(current, limit);
        QueryWrapper<SysTeacher> wrapper = new QueryWrapper<>();
        String tecName = teacherDto.getTecName();
        Long jobNum = teacherDto.getJobNum();
        String departmentName = teacherDto.getDepartmentName();
        String professionalName = teacherDto.getProfessionalName();


        if (!StringUtils.isEmpty(tecName)) {
            wrapper.like("tec_name", tecName);
        }
        if (!StringUtils.isEmpty(jobNum)) {
            wrapper.like("job_num",jobNum);
        }
        if (!StringUtils.isEmpty(departmentName)) {
            wrapper.like("department_name",departmentName);
        }
        if (!StringUtils.isEmpty(professionalName)) {
            wrapper.like("professional_name",professionalName);
        }

        IPage<SysTeacher> sysTeacherIPage = teacherService.pageTeacher(pageTeacher, wrapper);
        List<SysTeacher> records = sysTeacherIPage.getRecords();
        long total = sysTeacherIPage.getTotal();

        return Result.ok().data("total", total).data("records", records);
    }


    @PostMapping("addTeacher")
    @ApiOperation("添加教师")
    public Result addTeacher(@RequestBody SysTeacher sysTeacher,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = teacherService.addTeacher(sysTeacher);
        return result;
    }


    @GetMapping("getTeacherById/{id}")
    @ApiOperation("查询教师信息，修改回显")
    public Result getTeacherById(@PathVariable String id,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        SysTeacher teacherById = teacherService.getTeacherById(id);
        return Result.ok().data("item", teacherById);
    }

    @PostMapping("updateTeacher")
    @ApiOperation("修改教师信息")
    public Result updateTeacher(@RequestBody SysTeacher sysTeacher,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = teacherService.updateTeacher(sysTeacher);
        return result;
    }

    @DeleteMapping("delTeacher/{id}")
    @ApiOperation("删除教师信息")
    public Result delTeacher(@PathVariable String id,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        QueryWrapper<SysTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        /*SysTeacher one = teacherService.getOne(wrapper);
        Long jobNum = one.getJobNum();
        QueryWrapper<SysCourse> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("job_num",jobNum);
        courseMapper.selectCount()*/

        boolean remove = teacherService.remove(wrapper);
        if (remove) {
            return Result.ok().message("删除成功！");
        } else {
            return Result.error().message("删除失败！");
        }
    }

    @GetMapping("getTeacherByProfessionalName/{professionalName}")
    @ApiOperation("查询教师信息，修改回显")
    public Result getTeacherByProfessionalName(@PathVariable String professionalName, HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        List<SysTeacher> teacherByProfessionalName = teacherService.getTeacherByProfessionalName(professionalName);
        return Result.ok().data("teachers", teacherByProfessionalName);
    }


}

